package com.jvyou.app.common.utils.orm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jvyou.app.common.domain.model.BasePageInfo;


/**
 * Wrapper排序工具类
 *
 * @author 橘柚
 * @since 2024/11/1 15:56
 */
public class WrapperSortHelper {

    private static final String ASCEND = "ascend";

    private static final String DESCEND = "descend";

    public static <T> void applySort(QueryWrapper<T> wrapper, BasePageInfo pageInfo) {
        if (pageInfo == null || pageInfo.getSort() == null || pageInfo.getSort().isEmpty()) {
            return;
        }
        String sort = pageInfo.getSort();
        if (!sort.contains("-")) {
            return;
        }
        String[] sortArr = sort.split(",");
        for (String s : sortArr) {
            if (!s.contains("-")) {
                continue;
            }
            String[] sortItem = s.split("-");
            if (sortItem.length != 2) {
                continue;
            }
            switch (sortItem[1]) {
                case ASCEND:
                    wrapper.orderByAsc(sortItem[0]);
                    break;
                case DESCEND:
                    wrapper.orderByDesc(sortItem[0]);
                    break;
                default:
                    break;
            }
        }
    }
}